package com.yt.user.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

import com.yt.user.models.service.IAccountInfoService;
import com.yt.user.models.bo.AccountInfoSaveBO;
import com.yt.user.models.bo.AccountInfoBO;
import com.yt.user.models.dto.AccountInfoDTO;

/**
 * 账号信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:37:39
 */

@RestController
@RequestMapping("/cms/accountInfo")
@Slf4j
@Api(value = "AccountInfoController",tags = "账号信息接口")
public class AccountInfoController {

    @DubboReference
    private IAccountInfoService accountInfoService;

    @ApiOperation(value = "添加账号信息",notes = "添加账号信息")
    @PostMapping(value = "/insert", consumes = "application/json")
    public Result insert(@RequestBody AccountInfoSaveBO saveBO) {
        boolean flag = accountInfoService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存账号信息",notes = "保存账号信息")
    public Result save(AccountInfoSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改账号信息",notes = "修改账号信息")
    @PostMapping("/update")
    public Result update(@RequestBody AccountInfoSaveBO saveBO){
        // 检查参数
        if (saveBO == null || saveBO.getId() == null || saveBO.getId().isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002, "账号ID不能为空");
        }
        
        AccountInfoDTO dto = new AccountInfoDTO();
        // 设置主键字段
        dto.setId(saveBO.getId());
        
        // 复制其他属性
        dto.setPreDeposit(saveBO.getPreDeposit());
        dto.setTotalConsumption(saveBO.getTotalConsumption());
        dto.setMonthConsumption(saveBO.getMonthConsumption());
        dto.setIsArrears(saveBO.getIsArrears());
        dto.setArrearsAmount(saveBO.getArrearsAmount());
        dto.setPhone(saveBO.getPhone());
        // 手动设置时间字段
        dto.setUpdateTime(new java.util.Date());
        
        System.out.println("更新的DTO对象: " + dto);
        System.out.println("id值: " + dto.getId());
        boolean flag = accountInfoService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除账号信息",notes = "删除账号信息")
    @ApiImplicitParam(name = "id",value = "账号信息id",required = true,paramType = "query",dataType = "String")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") String id){
        boolean flag = accountInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除账号信息",notes = "批量删除账号信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        accountInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "String")
    public Result<AccountInfoDTO> get(@RequestParam("id") String id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        AccountInfoDTO dto = accountInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "账号信息列表",notes = "账号信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<AccountInfoDTO>> queryPageList(AccountInfoBO bo){
        Wrapper<AccountInfoDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<AccountInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = accountInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    
    @ApiOperation(value = "查询所有账号信息",notes = "查询所有账号信息")
    @PostMapping("/listAll")
    public Result<Page<AccountInfoDTO>> listAll(){
        Page<AccountInfoDTO> page = new Page<>(1, 10000);
        page = accountInfoService.selectPage(page);
        return new Result<>(page);
    }
}